//
//  MineViewController.m
//  Bees
//
//  Created by NowYouCanSee on 2018/12/27.
//  Copyright © 2018 xiaohan. All rights reserved.
//

#import "MineViewController.h"
#import "MineHeadView.h"
#import "XManagementViewWalletController.h"
#import "SettingViewController.h"
#import "LoginViewController.h"
#import "MineDataViewController.h"
#import "FeedbackViewController.h"
#import "AboutUsViewController.h"
#import "TransactionRecord.h"
#import "TheOverviewViewController.h"
static NSString*const kMineHeadViewReuseIdentifier = @"MineHeadViewReuseIdentifier";
@interface MineViewController ()

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)setupUI{
    self.navigationBarHidden = YES;
    self.customNavigationBar.height=kStatusBarHeight;
    [self.tableView registerClass:[MineHeadView class] forHeaderFooterViewReuseIdentifier:kMineHeadViewReuseIdentifier];
    self.tableView.contentInset = UIEdgeInsetsMake(AdaptedWidth(30), 0, 0, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    LMJWordArrowItem *item0 = [LMJWordArrowItem itemWithTitle:XLocalized(@"资产总览") subTitle:@""];
    item0.image=IMAGE_NAMED(@"item0");
    item0.subTitleColor = kFontColor2;
    item0.cellHeight = 60.0f;
    item0.destVc = TheOverviewViewController.class;
    LMJWordArrowItem *item1 = [LMJWordArrowItem itemWithTitle:XLocalized(@"交易记录") subTitle: @""];
    item1.image=IMAGE_NAMED(@"item1");
    item1.cellHeight = 60.0f;
    item1.destVc = TransactionRecord.class;
    LMJWordArrowItem *item2 = [LMJWordArrowItem itemWithTitle:XLocalized(@"钱包管理") subTitle: @""];
    item2.image=IMAGE_NAMED(@"item2");
    item2.cellHeight = 60.0f;
    item2.destVc = [XManagementViewWalletController class];
    
    LMJWordArrowItem *item3 = [LMJWordArrowItem itemWithTitle:XLocalized(@"系统设置") subTitle: @""];
    item3.image=IMAGE_NAMED(@"item3");
    item3.cellHeight = 60.0f;
    item3.destVc = SettingViewController.class;
    LMJWordArrowItem *item4 = [LMJWordArrowItem itemWithTitle:XLocalized(@"意见反馈") subTitle: @""];
    item4.image=IMAGE_NAMED(@"item4");
    item4.cellHeight = 60.0f;
    item4.destVc = FeedbackViewController.class;
    LMJWordArrowItem *item5 = [LMJWordArrowItem itemWithTitle:XLocalized(@"关于我们") subTitle: @""];
    item5.image=IMAGE_NAMED(@"item5");
    item5.cellHeight = 60.0f;
    item5.destVc = AboutUsViewController.class;
    LMJItemSection *section = [LMJItemSection sectionWithItems:@[item0,item1] andHeaderTitle:nil footerTitle:nil];
    LMJItemSection *section1 = [LMJItemSection sectionWithItems:@[item2,item3,item4,item5] andHeaderTitle:nil footerTitle:nil];
    [self.sections addObject:section];
    [self.sections addObject:section1];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        if (CURRENT_WALLET_ID) {
            return [[self.sections firstObject].items count];
        }else{
            return 0;
        }
    }
   return [super tableView:tableView numberOfRowsInSection:section];
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView *line = [cell viewWithTag:1000];
    if (!line) {
        line = [UIView new];
        line.backgroundColor = kLineColor;
        line.frame = CGRectMake(20, cell.height-CGFloatToPixel(.2), cell.width-20,CGFloatToPixel(.2));
        line.tag = 1000;
        [cell.contentView addSubview:line];
    }
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        MineHeadView *headV = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kMineHeadViewReuseIdentifier];
        headV.isLogin = !IsNilOrNull(kCurUser);
        kWeakSelf(self);
        headV.BackClick = ^{
            if (IsNilOrNull(kCurUser)) {
                [weakself.navigationController hh_pushBackViewController:[LoginViewController new]];
            }
        };
        headV.EditClick = ^{
            if (!IsNilOrNull(kCurUser)) {
                [weakself.navigationController pushViewController:[MineDataViewController new] animated:YES];
            }
        };
        return headV;
    }
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return AdaptedWidth(90.f);
    }
    return .01f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return .01f;
}
@end
